﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit.Electric.Common;

namespace HYElectricSetting
{
	public class CableTrayFittingSettingData
	{
		public static List<CableTrayFittingEntity> CableTrayFittingEntitys
		{
			get
			{
				return CableTrayFittingSettingData.cableTrayFittingEntitys;
			}
			set
			{
				CableTrayFittingSettingData.cableTrayFittingEntitys = value;
			}
		}

		public static List<VerticalCableTrayFittingEntity> VerticalCableTrayFittingEntity
		{
			get
			{
				return CableTrayFittingSettingData.verticalCableTrayFittingEntity;
			}
			set
			{
				CableTrayFittingSettingData.verticalCableTrayFittingEntity = value;
			}
		}

		public static void SaveCableTrayFittingSettingData(Autodesk.Revit.DB.Document doc, string path, XElement root, List<Element> cableTrayTypes)
		{
			bool flag = root == null;
			if (flag)
			{
				root = new XElement("Root");
			}
			else
			{
				root.RemoveAll();
			}
			foreach (Element element in cableTrayTypes)
			{
				CableTrayType cableTrayType = element as CableTrayType;
				foreach (CableTrayFittingEntity cableTrayFittingEntity in CableTrayFittingSettingData.CableTrayFittingEntitys)
				{
					bool flag2 = cableTrayFittingEntity.CableTrayType == cableTrayType.Name && cableTrayFittingEntity.CableTray == cableTrayType.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
					if (flag2)
					{
						CableTrayFittingSettingData.UpdateCableTrayFittingSetToRevit(doc, cableTrayType, cableTrayFittingEntity);
						break;
					}
				}
				bool flag3 = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "无配件的电缆桥架";
				if (!flag3)
				{
					foreach (VerticalCableTrayFittingEntity verticalCableTrayFittingEntity in CableTrayFittingSettingData.verticalCableTrayFittingEntity)
					{
						bool flag4 = verticalCableTrayFittingEntity.CableTrayType == cableTrayType.Name;
						if (flag4)
						{
							XElement xelement = new XElement("CableTrayType", new XAttribute("Type", verticalCableTrayFittingEntity.CableTrayType));
							XElement content = new XElement(VerticalCableTrayFittingType.LeftDownSideElbow.ToString(), (verticalCableTrayFittingEntity.LeftDownSideElbow == null) ? "" : verticalCableTrayFittingEntity.LeftDownSideElbow);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.RightDownSideElbow.ToString(), (verticalCableTrayFittingEntity.RightDownSideElbow == null) ? "" : verticalCableTrayFittingEntity.RightDownSideElbow);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.LeftUpSideElbow.ToString(), (verticalCableTrayFittingEntity.LeftUpSideElbow == null) ? "" : verticalCableTrayFittingEntity.LeftUpSideElbow);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.RightUpSideElbow.ToString(), (verticalCableTrayFittingEntity.RightUpSideElbow == null) ? "" : verticalCableTrayFittingEntity.RightUpSideElbow);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalUpTee.ToString(), (verticalCableTrayFittingEntity.VerticalUpTee == null) ? "" : verticalCableTrayFittingEntity.VerticalUpTee);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalDownTee.ToString(), (verticalCableTrayFittingEntity.VerticalDownTee == null) ? "" : verticalCableTrayFittingEntity.VerticalDownTee);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalUpSideTee.ToString(), (verticalCableTrayFittingEntity.VerticalUpSideTee == null) ? "" : verticalCableTrayFittingEntity.VerticalUpSideTee);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalDownSideTee.ToString(), (verticalCableTrayFittingEntity.VerticalDownSideTee == null) ? "" : verticalCableTrayFittingEntity.VerticalDownSideTee);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalUpCornerTee.ToString(), (verticalCableTrayFittingEntity.VerticalUpCornerTee == null) ? "" : verticalCableTrayFittingEntity.VerticalUpCornerTee);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalDownCornerTee.ToString(), (verticalCableTrayFittingEntity.VerticalDownCornerTee == null) ? "" : verticalCableTrayFittingEntity.VerticalDownCornerTee);
							xelement.Add(content);
							content = new XElement(VerticalCableTrayFittingType.VerticalUpCornerCross.ToString(), (verticalCableTrayFittingEntity.VerticalUpCornerCross == null) ? "" : verticalCableTrayFittingEntity.VerticalUpCornerCross);
							xelement.Add(content);
							root.Add(xelement);
							break;
						}
					}
				}
			}
			root.Save(path);
		}

		private static void UpdateCableTrayFittingSetToRevit(Autodesk.Revit.DB.Document doc, CableTrayType ctt, CableTrayFittingEntity cfe)
		{
			Transaction transaction = new Transaction(doc, "SaveFittingData");
			transaction.Start();
			string a = ctt.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString();
			CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM, cfe.HorizontalElbow);
			CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM, cfe.VerticalInnerElbow);
			CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM, cfe.VerticalOuterElbow);
			CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM, cfe.Transition);
			CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_UNION_PARAM, cfe.Union);
			bool flag = a == "带配件的电缆桥架";
			if (flag)
			{
				CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM, cfe.HorizontalTee);
				CableTrayFittingSettingData.UpdateCableTrayFiting(doc, ctt, BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM, cfe.HorizontalCross);
			}
			transaction.Commit();
		}

		private static void UpdateCableTrayFiting(Autodesk.Revit.DB.Document doc, CableTrayType ctt, BuiltInParameter param, string value)
		{
			bool flag = string.IsNullOrEmpty(value);
			if (!flag)
			{
				string b = value.Substring(0, value.LastIndexOf(":"));
				IList<Element> list = RevitFilter.GetElementsOfType(doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_CableTrayFitting).ToElements();
				foreach (Element element in list)
				{
					FamilySymbol familySymbol = element as FamilySymbol;
					bool flag2 = familySymbol.Family.Name == b;
					if (flag2)
					{
						ctt.get_Parameter(param).Set(familySymbol.Id);
						break;
					}
				}
			}
		}

		private static List<CableTrayFittingEntity> cableTrayFittingEntitys = new List<CableTrayFittingEntity>();

		private static List<VerticalCableTrayFittingEntity> verticalCableTrayFittingEntity = new List<VerticalCableTrayFittingEntity>();
	}
}
